package com.epro.dx.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.adapter.IdentitiesAdapter;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.util.StringUtil;

/**
 * @author TonyYang
 * 描述：第一次加载程序展示版本新特性的页面
 */
public class ShowNewIdentities extends Activity {
	
	//展示广告
	private ViewPager mViewPager;
	//要展示的图片资源ID
//	private String[] imgPath;
	private int[] imgIDs;
	//完成按钮
	private TextView finish ;
	//标准圆点的容器
//	private LinearLayout pointContainer;
	//红点
//	private View redPoint;
	private IdentitiesAdapter identitiesAdapter;
	
	private Handler handler=new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//集成测试GA
		Tracker tracker = ((DXApplication)getApplication()).getTracker(TrackerName.APP_TRACKER);
		tracker.setScreenName("NewIdentities");
		tracker.send(new HitBuilders.ScreenViewBuilder().build());
		tracker.setScreenName(null);
		initView();
		initData();
		initEvent();
//		handler.postDelayed(toHomeRunable, 3000);
	}
	
//	Runnable toHomeRunable=new Runnable() {
//		
//		@Override
//		public void run() {
//			goToHome();
//		}
//	};
	
	@Override
	protected void onStop() {
//		handler.removeCallbacks(toHomeRunable);
		super.onStop();
	}
	
	private void goToHome(){
		// 保存当前的版本号
		PackageManager manager = ShowNewIdentities.this.getPackageManager();
		String versionCode = null;
		try {
			PackageInfo packageInfo = manager.getPackageInfo(ShowNewIdentities.this.getPackageName(), 0);
			versionCode = packageInfo.versionCode+"";
		} catch (NameNotFoundException e) {
			versionCode = "1.0";
		}
		StringUtil.saveConfig("VersionCode", versionCode);
		//跳转到主页面
		Intent intent = new Intent(ShowNewIdentities.this, HomeActivity.class);
        startActivity(intent);
        ShowNewIdentities.this.overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
        finish();  
	}

	/**
	 * 加载事件
	 */
	private void initEvent() {
		
		// 点击按钮完成展示，保存状态并进入主页
		finish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				goToHome();
			}
		});
		//ViewPager页面滑动时动态移动红点
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				//设置按钮在最后一张显示出来
				finish.setVisibility(position == (imgIDs.length-1) ? View.VISIBLE : View.INVISIBLE);
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset, int arg2) {
				// 当页面滑动时，红点随之移动
//				int pointMovePx = DpPxUtils.Dp2Px(getApplicationContext(), 20);
//				float leftMargin = pointMovePx*(position+positionOffset);
//				RelativeLayout.LayoutParams redpointParams = (android.widget.RelativeLayout.LayoutParams) redPoint.getLayoutParams();
//				redpointParams.leftMargin = Math.round(leftMargin);
//				redPoint.setLayoutParams(redpointParams);
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	/**
	 * 加载初始化数据
	 */
	private void initData() {
//		imgPath =new String[]{"whatsnew1.png","whatsnew2.png,whatsnew3.png"};
		imgIDs = new int[]{R.drawable.whatsnew1,R.drawable.whatsnew2,R.drawable.whatsnew3,R.drawable.whatsnew4,R.drawable.whatsnew5};
		identitiesAdapter = new IdentitiesAdapter(getApplicationContext(), imgIDs);
		mViewPager.setAdapter(identitiesAdapter);
		//根据要展示的图片数量添加默认圆点
//		for(int i = 0;i<imgPath.length;i++){
//			View point = new View(getApplicationContext());
//			point.setBackgroundResource(R.drawable.bg_white_circle);
//			LayoutParams params = 
//					new LayoutParams(DpPxUtils.Dp2Px(getApplicationContext(), 10), 
//							DpPxUtils.Dp2Px(getApplicationContext(), 10));
//			if(i!=0){
//				params.leftMargin = DpPxUtils.Dp2Px(getApplicationContext(), 10);
//			}
//			point.setLayoutParams(params);
//			pointContainer.addView(point);
//		}
	}

	/**
	 * 加载视图
	 */
	private void initView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_shownewidentities);
		mViewPager = (ViewPager) findViewById(R.id.viewpager_showidentities);
		finish = (TextView) findViewById(R.id.btn_showidentities_finish);
//		pointContainer = (LinearLayout) findViewById(R.id.point_normal_contain_showidentified);
//		redPoint = findViewById(R.id.redpoint_showidentified);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//回收图片
		if(identitiesAdapter!=null){
			identitiesAdapter.reclyImage();
		}
	}

	/**
	 * 屏蔽返回键
	 */
	@Override
	public void onBackPressed() {
	}
}
